home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / Tape Stuff / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-05  |  3.4 KB  |  218 lines  |  [TEXT/KAHL]

  1. /* main.c */
  2.  
  3. #include <assert.h>
  4.  
  5. #include "SCSIDefines.h"
  6. #include "Defines.h"
  7. #include "Prototypes.h"
  8.  
  9. MenuHandle gAppleMenu, gFileMenu, gEditMenu;        /* menus */
  10. Boolean gDone;                                        /* done flag */
  11. EventRecord gTheEvent;                                /* event record */
  12.  
  13. void main(void)
  14. {
  15.     /* set up minimum stack space */
  16.     
  17.     SetApplLimit(GetApplLimit() - MIN_STACK);
  18.     assert(StackSpace() >= MIN_STACK);
  19.     
  20.     ToolBoxInit();
  21.     MenuBarInit();
  22.     Initialize();
  23.     MainLoop();
  24. }
  25.  
  26. void MenuBarInit()
  27. {
  28.     Handle myMenuBar;
  29.     
  30.     myMenuBar=GetNewMBar(MENU_BAR_ID);
  31.     SetMenuBar(myMenuBar);
  32.     
  33.     gAppleMenu=GetMHandle(APPLE_MENU_ID);
  34.     AddResMenu(gAppleMenu,'DRVR');
  35.     
  36.     gFileMenu=GetMHandle(FILE_MENU_ID);
  37.     gEditMenu=GetMHandle(EDIT_MENU_ID);
  38.     
  39.     DrawMenuBar();
  40. }    
  41.  
  42. void ToolBoxInit(void)
  43. {
  44.     InitGraf(&thePort);
  45.     InitFonts();
  46.     FlushEvents(everyEvent, 0);
  47.     InitWindows();
  48.     InitMenus();
  49.     TEInit();
  50.     InitDialogs(NULL);
  51.     InitCursor();
  52. }
  53.  
  54.     
  55. void MainLoop()
  56. {
  57.     char theChar;                    /* key character */
  58.  
  59.     gDone=FALSE;
  60.     while(gDone==FALSE)
  61.     {
  62.     
  63.         /* get events */
  64.     
  65.         WaitNextEvent(everyEvent,&gTheEvent,MIN_SLEEP,NIL_MOUSE_REGION);
  66.     
  67.         /* and act on it */
  68.     
  69.         switch (gTheEvent.what)
  70.         {
  71.         case mouseDown:
  72.             HandleMouseDown();
  73.             break;
  74.         
  75.         case keyDown:
  76.         case autoKey:
  77.             theChar=gTheEvent.message & charCodeMask;
  78.             if ((gTheEvent.modifiers & cmdKey)!=0)
  79.             {
  80.                 AdjustMenus();
  81.                 HandleMenuChoice(MenuKey(theChar));
  82.             }
  83.             break;
  84.         
  85.         case updateEvt:
  86.             break;
  87.         }    
  88.     }                
  89. }
  90.  
  91. void HandleMouseDown()
  92. {
  93.     WindowPtr whichWindow;
  94.     short thePart;
  95.     long menuChoice,windSize;
  96.     
  97.     thePart=FindWindow(gTheEvent.where,&whichWindow);
  98.     switch (thePart)
  99.     {
  100.     case inMenuBar:
  101.         AdjustMenus();
  102.         menuChoice=MenuSelect(gTheEvent.where);
  103.         HandleMenuChoice(menuChoice);
  104.         break;
  105.         
  106.     case inSysWindow:
  107.         SystemClick(&gTheEvent,whichWindow);
  108.         break;
  109.     }
  110. }        
  111.  
  112. void AdjustMenus()
  113. {
  114.     /* check for desk accessories */
  115.     
  116.     if (IsDAWindow(FrontWindow()))
  117.     {
  118.         EnableItem(gEditMenu,UNDO_ITEM);
  119.         EnableItem(gEditMenu,CUT_ITEM);
  120.         EnableItem(gEditMenu,COPY_ITEM);
  121.         EnableItem(gEditMenu,PASTE_ITEM);
  122.         EnableItem(gEditMenu,CLEAR_ITEM);
  123.     }
  124.     else
  125.     {
  126.         DisableItem(gEditMenu,UNDO_ITEM);
  127.         DisableItem(gEditMenu,CUT_ITEM);
  128.         DisableItem(gEditMenu,COPY_ITEM);
  129.         DisableItem(gEditMenu,PASTE_ITEM);
  130.         DisableItem(gEditMenu,CLEAR_ITEM);
  131.     }
  132.     
  133. }
  134.  
  135. short IsDAWindow(whichWindow)
  136. WindowPtr whichWindow;
  137. {
  138.     if (whichWindow==NULL) return (FALSE);
  139.     else /* DA windows have negative windowKinds */
  140.       return(((WindowPeek)whichWindow)->windowKind<0);
  141. }      
  142.  
  143. void HandleMenuChoice(menuChoice)
  144. long menuChoice;
  145. {
  146.     short theMenu;
  147.     short theItem;
  148.     
  149.     if (menuChoice!=0)
  150.     {
  151.         theMenu=HiWord(menuChoice);
  152.         theItem=LoWord(menuChoice);
  153.         switch (theMenu)
  154.         {
  155.         case APPLE_MENU_ID:
  156.             HandleAppleChoice(theItem);
  157.             break;
  158.             
  159.         case FILE_MENU_ID:
  160.             HandleFileChoice(theItem);
  161.             break;
  162.             
  163.         case EDIT_MENU_ID:
  164.             HandleEditChoice(theItem);
  165.             break;
  166.         }
  167.         HiliteMenu(0);
  168.     }
  169. }        
  170.                     
  171. void HandleAppleChoice(theItem)
  172. short theItem;
  173. {
  174.     Str255 accName;
  175.     short accNumber;
  176.     
  177.     switch (theItem)
  178.     {
  179.     case ABOUT_ITEM:
  180.         NoteAlert(ABOUT_ID,NULL);
  181.         break;
  182.         
  183.     default:
  184.         GetItem(gAppleMenu,theItem,accName);
  185.         accNumber=OpenDeskAcc(accName);
  186.         break;
  187.     }
  188. }        
  189.  
  190. void HandleEditChoice(theItem)
  191. short theItem;
  192. {
  193.     SystemEdit(theItem-1);
  194. }    
  195.  
  196. void HandleFileChoice(theItem)
  197. short theItem;
  198. {
  199.     switch (theItem)
  200.     {
  201.     case READ_TAPE_ITEM:
  202.     case LIST_TAPE_ITEM:
  203.     case WRITE_TAPE_ITEM:
  204.     case DUMP_TAPE_ITEM:
  205.     case DUMP_FILE_ITEM:
  206.         DoWork(theItem);
  207.         break;
  208.     
  209.     case QUIT_ITEM:
  210.         gDone=TRUE;
  211.         break;
  212.     }
  213. }                    
  214.  
  215.         
  216.  
  217.  
  218.